package com.tyrone.algorithm.graph;


import com.tyrone.algorithm.linear.Stack;

/**
 *  拓扑排序
 */
public class TopoLogical {
    private Stack<Integer> order;//顶点的拓扑排序
    public TopoLogical(Digraph G){
        DirectedCycle directedCycle = new DirectedCycle(G);
        if (!directedCycle.hasCycle()){
            DepthFirstOrder depthFirstOrder = new DepthFirstOrder(G);
            order=depthFirstOrder.reversePost();
        }
    }
    /**
     * 判断图G是否有环
     * @return
     */
    public boolean isCycle() {
        return order==null;
    }

    /**
     * 获取拓扑排序的所有顶点
     * @return
     */
    public Stack<Integer> order(){
        return order;
    }

}
